package com.corder.display.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;

/*
 * 密码工具
 */
public class EncryptionTool {

	private static final char hexDigits[] = { '0', '1', '2', '3', '4', '5',
			'6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
	private static MessageDigest md = null;
	public static final int g = 2;

	public static Long createPrimaryKey() {

		return (long) (Math.pow(g, new Random().nextInt(31)) % createRadomPrimeNunber(4));
	}

	/**
	 * MD5转换器
	 */
	public static String makeMD5(String str) {
		char tmp[] = null;
		try {
			md = MessageDigest.getInstance("MD5");
			byte[] btInput = str.getBytes();
			md.update(btInput);
			btInput = md.digest();
			int j = btInput.length;
			tmp = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; i++) {
				byte byte0 = btInput[i];
				tmp[k++] = hexDigits[byte0 >>> 4 & 0xf];
				tmp[k++] = hexDigits[byte0 & 0xf];
			}
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new String(tmp);
	}

	/**
	 * 根据n输出对应位数的素数
	 */
	public static long createRadomPrimeNunber(int n) {
		long recLong = 0;
		List<Long> list = listAllPrimeNumber(n);
		Random rd = new Random();
		int randomIndex = Math.abs(rd.nextInt() % list.size());
		recLong = ((Long) list.get(randomIndex)).longValue();
		return recLong;
	}

	public static List<Long> listAllPrimeNumber(int n) {
		List<Long> list = new ArrayList<Long>();
		long low = (long) Math.pow(10, n - 1);
		long high = (long) Math.pow(10, n) - 1;
		for (long i = low; i < high; i++) {
			if (isSushu(i)) {
				list.add(new Long(i));
			}
		}
		return list;
	}

	public static boolean isSushu(long x) {// 定义一个判断一个数是否是素数的函数
		if (x < 2)
			return false;
		if (x == 2)
			return true;
		for (long i = 2; i <= (long) Math.sqrt(x); i++)
			if (x % i == 0)
				return false;
		return true;
	}

	public static void main(String[] args) {
		double p = createRadomPrimeNunber(4);
		int g = 2;
		int Xa = new Random().nextInt(8);
		int Xb = new Random().nextInt(8);
		int Ya = (int) ((Math.pow(g, Xa)) % p);
		int Yb = (int) ((Math.pow(g, Xb)) % p);
		int k1 = (int) ((Math.pow(Ya, Xb)) % p);
		int k2 = (int) ((Math.pow(Yb, Xa)) % p);

		String str = "hellow word!";
		byte[] bytes = str.getBytes();
		for (int i = 0; i < bytes.length; i++) {
			bytes[i] += k1;
		}
		System.out.println("加密后：" + new String(bytes));
		for (int i = 0; i < bytes.length; i++) {
			bytes[i] -= k2;
		}
		System.out.println("解密后：" + new String(bytes));
		System.out.println("md5:" + makeMD5("admin"));
	}

	/**
	 * 获得一个UUID
	 * 
	 * @return String UUID
	 */
	public static String getUUID() {
		String s = UUID.randomUUID().toString();
		// 去掉“-”符号
		return s.substring(0, 8) + s.substring(9, 13) + s.substring(14, 18)
				+ s.substring(19, 23) + s.substring(24);
	}

	/**
	 * 获得指定数目的UUID
	 * 
	 * @param number
	 *            int 需要获得的UUID数量
	 * @return String[] UUID数组
	 */
	public static String[] getUUID(int number) {
		if (number < 1) {
			return null;
		}
		String[] ss = new String[number];
		for (int i = 0; i < number; i++) {
			ss[i] = getUUID();
		}
		return ss;
	}

	/**
	 * 生成指定长度的String
	 */
	public static String getRandomString(int pwd_len) {
		// 35是因为数组是从0开始的，26个字母+10个数字
		final int maxNum = 36;
		int i; // 生成的随机数
		int count = 0; // 生成的密码的长度
		char[] str = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
				'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
				'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

		StringBuffer pwd = new StringBuffer("");
		Random r = new Random();
		while (count < pwd_len) {
			// 生成随机数，取绝对值，防止生成负数，

			i = Math.abs(r.nextInt(maxNum)); // 生成的数最大为36-1

			if (i >= 0 && i < str.length) {
				pwd.append(str[i]);
				count++;
			}
		}
		return pwd.toString();
	}
}
